home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10066 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  90 lines

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Encapsulation question.
  5. Date: 5 Mar 1996 22:24:08 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4hieu8$hfh@news1.usa.pipeline.com>
  8. References: <4hg701$goi@insosf1.netins.net>
  9. NNTP-Posting-Host: pipe16.h1.usa.pipeline.com
  10. X-PipeUser: grantp
  11. X-PipeHub: usa.pipeline.com
  12. X-PipeGCOS: (Pete)
  13. X-Newsreader: Pipeline v3.5.0
  14.  
  15. On Mar 05, 1996 00:59:35 in article <Encapsulation question.>,
  16. 'hhowe@trgnet.com (Harold Howe)' wrote: 
  17.  
  18.  
  19. >Greetings.  
  20. >Does the following violate encapsulation?  Should an object tell a private
  21.  
  22. >member the address of other private members?  I am using this on a grander
  23.  
  24. >scale, in which the Application class tells a private dialog object the  
  25. >address of a private configuration structure?  The dialog class needs
  26. access  
  27. >to the structure so it can modify it, and this is how I tell the dialog
  28. where  
  29. >to look.  Aside from using globals, does anyone have any better
  30. suggestions? 
  31. >   
  32. >class Application 
  33. >{ 
  34. >private: 
  35. >int j; 
  36. >Dialog *dlg; 
  37. >public: 
  38. >Application(void)  { dlg = new Dialog(&j);  } 
  39. >} 
  40. >class Dialog 
  41. >{ 
  42. >private: 
  43. >int *ptr; 
  44. >... // plus other members which access ptr 
  45. >public: 
  46. >Dialog(int *addr) {  ptr = addr } ; 
  47. >} 
  48. >int main() 
  49. >{ 
  50. >Application app; 
  51. >} 
  52.  
  53. Pass a reference to Application to Dialog consructor instead.  Then use 
  54. the object to access the members.  In order to do this, you must one of: 
  55.  
  56.   *  make appropriate members of public 
  57.   *  declare Application a friend of Dialog 
  58.   *  Provide public Get/Set accessors. 
  59.  
  60.  
  61. class Application; 
  62.  
  63. class Dialog 
  64.  { 
  65.    private: 
  66.       Application & app; 
  67.    public: 
  68.      Dialog(Application& app) : app(app) {} 
  69.      void somefunc() { dosomething(app.someslot); } 
  70.  }; 
  71.  
  72. class Application 
  73.  { 
  74.   private: 
  75.      int j; 
  76.      Dialog *dlg; 
  77.    public: 
  78.      Application()  { dlg = new Dialog(*this);  } 
  79.  }; 
  80.  
  81.  
  82. -- 
  83. Pete Grant 
  84. Kalevi, Inc. 
  85. Software Engineering & development
  86.